package com.wing.robot.controller.admin;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;
import com.alibaba.nacos.common.utils.StringUtils;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wing.robot.model.response.RobotStationVO;
import com.wing.web.annotation.RequestObjectFieldIgnore;
import com.wing.common.utils.JsonResult;
import com.wing.common.utils.PageResult;
import com.wing.robot.model.entity.RobotStation;
import com.wing.robot.model.request.RobotStationForm;
import com.wing.robot.service.RobotStationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import java.util.HashMap;
import java.util.Map;

/**
 * @author
 */
@Validated
@Api(value = "Admin-机器人工站管理", tags = "#/robot/robotStation")
@RestController("robotStationController")
@RequestMapping("robot/admin/robotStation")
@RequiredArgsConstructor
public class RobotStationController {

    private final RobotStationService robotStationService;

    @PostMapping(value = {"/info"})//rds测试接口
    public JsonResult getInfo(@RequestBody Map<Object, String> test) {
        System.out.println(test);
        System.out.println(test.get("agvId"));
        System.out.println(test.get("taskId"));
        return JsonResult.success("test", test);
    }

    @ApiOperation(value = "查询所有机器人工站", notes = "搜索")
    @GetMapping("/list")
    public PageResult<RobotStationVO> list(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer limit,
            @RequestParam(defaultValue = "create_time") String sort,
            @RequestParam(defaultValue = "asc") String order,
            String code, String name, Long id, RobotStation.Type type,String startCreateTime
        ,String endCreateTime) {
        Page<RobotStation> pg = new Page<>(page, limit);
        Map<String, Object> param = new HashMap<>();
        param.put("name", name);
        param.put("code", code);
        param.put("id", id);
        param.put("type", type);
        if (com.alibaba.nacos.common.utils.StringUtils.isNotBlank(startCreateTime)) {
            param.put("startCreateTime", startCreateTime);
        }
        if (StringUtils.isNotBlank(endCreateTime)) {
            param.put("endCreateTime", endCreateTime);
        }
        IPage<RobotStationVO> list = robotStationService.pageList4app(pg, param);
        return new PageResult<>(list.getRecords(), list.getTotal());
    }


    @ApiOperation(value = "添加机器人工站", notes = "添加")
    @RequestObjectFieldIgnore({"id", "createTime", "remark"})
    @PostMapping("/add")
    public JsonResult add(@Validated(RobotStationForm.Add.class) RobotStationForm robotStationForm) {
        if (robotStationService.add(robotStationForm) > 0) {
            return JsonResult.success("添加成功");
        }
        return JsonResult.fail("添加失败");
    }


    @ApiOperation(value = "修改机器人工站", notes = "修改")
    @PutMapping("/update")
    public JsonResult update(@Validated(RobotStationForm.Update.class) RobotStationForm robotStationForm) {
        RobotStation robotStation = robotStationService.getById(robotStationForm.getId());
        if (robotStation == null) {
            return JsonResult.fail("修改失败！");
        }
        CopyOptions copyOptions = CopyOptions.create()
                .setEditable(RobotStation.class)
                .setIgnoreError(true)
                .setIgnoreNullValue(true);
        BeanUtil.copyProperties(robotStationForm, robotStation, copyOptions);
        if (robotStationService.updateById(robotStation)) {
            return JsonResult.success("修改成功！");
        }
        return JsonResult.fail("修改失败！");
    }


    @ApiOperation(value = "删除机器人工站", notes = "删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "robotStationId", value = "机器人工站id", required = true, dataType = "Integer", paramType = "path")
    })
    @DeleteMapping("/{id}")
    public JsonResult delete(@PathVariable("id") Long robotStationId) {
        if (robotStationService.removeById(robotStationId)) {
            return JsonResult.success("删除成功");
        }
        return JsonResult.fail("删除失败");
    }
}
